package org.realogic.cfm.persistence.mybatis.interceptor;

import java.io.Serializable;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.plugin.Interceptor;
import org.realogic.cfm.persistence.dialect.Dialect;
import org.realogic.cfm.persistence.util.Reflections;

public abstract class BaseInterceptor implements Interceptor, Serializable {

	private static final long serialVersionUID = 4596430444388728543L;

	protected static final String DELEGATE = "delegate";

	protected static final String MAPPED_STATEMENT = "mappedStatement";

	protected Dialect DIALECT;

	/**
	 * 设置属性，支持自定义方言类和制定数据库的方式
	 * <p>
	 * <code>dialectClass</code>,自定义方言类。可以不配置这项 <ode>dbms</ode> 数据库类型，插件支持的数据库
	 * <code>sqlPattern</code> 需要拦截的SQL ID
	 * </p>
	 * 如果同时配置了<code>dialectClass</code>和<code>dbms</code>,则以<code>dbms</code>为主
	 * 
	 * @param p
	 */
	protected void initProperties(Properties p) {
		String dialectClass = p.getProperty("dialectClass");
		if (StringUtils.isEmpty(dialectClass)) {
			throw new IllegalStateException("数据库分页方言无法找到!");
		} else {
			Dialect dialect1 = (Dialect) Reflections.instance(dialectClass);
			if (dialect1 == null) {
				throw new IllegalStateException("方言实例错误");
			}
			DIALECT = dialect1;
		}
	}
}
